@echo off
setlocal enabledelayedexpansion

set "port=3306"
set "mysql_service_name=mysql"

:: Check if the MySQL service is running
for /f "tokens=2 delims=:" %%a in ('sc query !mysql_service_name! ^| find "RUNNING"') do (
    if "%%a" == "RUNNING" (
        echo MySQL service is already running.
        pause
        exit /b
    )
)

:: Kill the process listening on the specified port, if any
for /f "tokens=5 delims= " %%a in ('netstat -ano ^| findstr :%port% ') do (
    set "pid=%%a"
)

if defined pid (
    taskkill /F /PID !pid! >nul 2>&1
)

:: Start the MySQL service
net start !mysql_service_name!
if %errorlevel%==0 (
    echo MySQL service started successfully.
) else (
    echo Failed to start MySQL service.
)

pause
